import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:xr_base/view_util.dart';
import 'package:xr_dio/xr_dio.dart';

import '../apis/apis.dart';
import '../data/user_data.dart';

class LoginProvider extends ChangeNotifier {
  /// 发送验证码
  static getSmsCode({required String phone, required String occasionType}) async {
    HttpResponse appResponse = (await XrHttpClient.getInstance().get(Apis.getSmsCode,
        queryParameters: {'phone': phone, 'occasionType': occasionType, 'areaCode': '86'})) as HttpResponse;
    if (appResponse.ok) {
      if (ObjectUtil.isNotEmpty(appResponse.data)) {
        debugPrint("====发送验证码" + appResponse.data.toString());
      }
    } else {
      debugPrint("====" + appResponse.error.toString());
    }
  }

  /// 登录
  static login(
      {required String phone,
      required String smsCode,
      String occasionType = '1',
      CancelToken? cancelToken,
      ValueChanged<String>? onOk,
      ValueChanged<String>? onFail}) async {
    UserData().login(
        phone: phone,
        smsCode: smsCode,
        occasionType: occasionType,
        cancelToken: cancelToken,
        onOk: (String result) {
          showToast('登录成功');
          if (onOk != null) {
            onOk(result);
          }
        },
        onFail: (String error) {
          if (onFail != null) {
            onFail(error);
          }
          showToast(error);
        });
  }
}
